//
//  GSYMineViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "GSYMineViewController.h"
#import "GSYMineHeadView.h"
#import "GSYMineFootView.h"

@interface GSYMineViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
@property (nonatomic,strong)UITableView *myTableView;

@property (nonatomic,strong)GSYMineHeadView *HeadView;

@property (nonatomic,strong)GSYMineFootView *FootView;

@property (nonatomic,strong)NSMutableArray *BigArray;
@end

@implementation GSYMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.BigArray = [NSMutableArray arrayWithObjects:@"收藏",@"清除缓存", nil];
    
    self.view.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:243/255.0 alpha:1.0];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = @"我的";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 667) style:UITableViewStylePlain];
    
    self.myTableView.delegate = self;
    
    self.myTableView.dataSource = self;
    
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.myTableView];
    
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    self.HeadView = [[GSYMineHeadView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    
    self.myTableView.tableHeaderView = self.HeadView;
    
   
    self.FootView = [[GSYMineFootView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    
    self.myTableView.tableFooterView = self.FootView;
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 1;
        
    }
    else{
    
    return 1;
        
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *cellIdentifier = @"cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
            
        }
        
        cell.textLabel.text = [[self.BigArray objectAtIndex:0]description];
        
        return cell;

        
    }
    else
    {
        static NSString *cellIdentifier = @"cell";

        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
            
        }
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        
        NSString *messageStr = [NSString stringWithFormat:@"%.1fM", [self folderSizeAtPath:path]];

        cell.detailTextLabel.text = messageStr;
        
        cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:17];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = [[self.BigArray objectAtIndex:1]description];
        
        return cell;

    
    
    
    
    }
    
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    if (section == 1) {
        
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 20)];
        view1.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 100, 20)];
        
        label.text = @"   设置";
        
        label.font = [UIFont boldSystemFontOfSize:13];
        
        [view1 addSubview:label];
        
        return view1;

        
    }else
    {
    
    
        return nil;
    
    }
    
   

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    if (section == 1) {
        
        return 40;

        
    }else
    {
    
    
        return 0;
    
    }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        
        
        
    }
    else
    {
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清楚缓存吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        [alertView show];
        
    
    
    }



}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        //        根据路径清除缓存
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        
        [self clearCache:path];
        
        [self.myTableView reloadData];
        
        
        
    }else
    {
        
        
        
        
    }
    
    
}


//单个文件大小
-(long long)fileSizeAtPath:(NSString*)filePath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]){
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
    
}



/*整个目录的文件大小*/

-(float)folderSizeAtPath:(NSString *) folderPath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPath]) return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString* fileName;
    
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}
- (void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}



-(void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
    [self.myTableView reloadData];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
